package com.example.hans.navigationbyviewpageandradiobutton;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener{

    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private TextView textView1, textView2, textView3, textView4;
    private List<View> views;
    private List<TextView> textViewList;
    private MyPageAdapter myPageAdapter;
    LocalActivityManager localActivityManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        localActivityManager = new LocalActivityManager(this,true);

        localActivityManager.dispatchCreate(savedInstanceState);

        findView();


    }

    public void findView() {

        textViewList = new ArrayList<>();

        viewPager = findViewById(R.id.viewpager);
        viewPager.setOnPageChangeListener(this);




        radioGroup = findViewById(R.id.radiogroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                for (int i = 0; i < radioGroup.getChildCount(); i++) {
                    if (radioGroup.getChildAt(i).getId() == checkedId) {
                        viewPager.setCurrentItem(i);
                        whiltLineVisibility(i);
                        break;
                    }
                }
            }
        });

        textView1 = findViewById(R.id.txtv_buttom1);
        textView2 = findViewById(R.id.txtv_buttom2);
        textView3 = findViewById(R.id.txtv_buttom3);
        textView4 = findViewById(R.id.txtv_buttom4);
        textViewList.add(textView1);
        textViewList.add(textView2);
        textViewList.add(textView3);
        textViewList.add(textView4);
        setPage();


    }

    public void whiltLineVisibility(int position) {
        for (int i = 0; i < textViewList.size(); i++) {
            if (i == position) {
                textViewList.get(i).setVisibility(View.VISIBLE);
            } else {
                textViewList.get(i).setVisibility(View.INVISIBLE);
            }
        }
    }

    public void setPage() {
        views = new ArrayList<>();


        //用activity填充视图
        Intent in1 = new Intent(getApplicationContext(),FirstActivity.class);
        views.add(getView("first",in1));
        Intent in2 = new Intent(getApplicationContext(),SecondActivity.class);
        views.add(getView("second",in2));
        Intent in3 = new Intent(getApplicationContext(),ThirdActivity.class);
        views.add(getView("third",in3));
        Intent in4 = new Intent(getApplicationContext(),FourActivity.class);
        views.add(getView("four",in4));


        //用布局填充视图
//        LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
//
//        View view1 = inflater.inflate(R.layout.first,null);
//        View view2 = inflater.inflate(R.layout.second,null);
//        View view3 = inflater.inflate(R.layout.third,null);
//        View view4 = inflater.inflate(R.layout.four,null);
//
//        views.add(view1);
//        views.add(view2);
//        views.add(view3);
//        views.add(view4);



        myPageAdapter = new MyPageAdapter(views);
        viewPager.setAdapter(myPageAdapter);
        viewPager.setCurrentItem(0);

    }

    public View getView(String id,Intent intent) {
        return localActivityManager.startActivity(id,intent).getDecorView();
    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        whiltLineVisibility(i);

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
